package com.opac.readerhome.activity;

import harven.demo.capture.Capture;
import harven.demo.capture.Capture.CaptureListener;
import harven.demo.capture.Statices;
import harven.demo.capture.decoding.CaptureActivityHandler.HandlerActivityCallBack;
import harven.demo.capture.view.ViewfinderView;

import java.util.Vector;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.opac.readerhome.R;

public class QrActivity extends Activity {
	private Capture capture;
	
	private ViewfinderView viewfinderView; 
//	private Button cancelScanButton;
	private Button cancelImage;
	
	static Vector<BarcodeFormat> decodeFormats;
	static{
		decodeFormats = new Vector<BarcodeFormat>(2);
		decodeFormats.add(BarcodeFormat.QR_CODE);
//		decodeFormats.add(BarcodeFormat.EAN_8);
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capture);
		//ViewUtil.addTopView(getApplicationContext(), this, R.string.scan_card);
//		CameraManager.init(getApplication());
		SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
		viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
		viewfinderView.setFramingRectType(Statices.View_Framing_Type_Square);
//		cancelScanButton = (Button) this.findViewById(R.id.btn_cancel_scan);
		cancelImage =  (Button) this.findViewById(R.id.qrcodeBack);
		capture = new Capture(viewfinderView, surfaceView, new CallBack());
		capture.setDecodeFormats(decodeFormats);
		capture.setListener(new Listener());
	}

	@Override
	protected void onResume() {
		super.onResume();
		capture.restartCapture();
		//quit the scan view
//		cancelScanButton.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				Intent mIntent = new Intent("OnSuccess");  
//		        mIntent.putExtra("qrcode", "canceled");  
//		        //发送广播  
//		        sendBroadcast(mIntent);  
//				QrActivity.this.finish();
//			}
//		});
		cancelImage.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent mIntent = new Intent("OnSuccess");  
				mIntent.putExtra("qrcode", "canceled");  
				//发送广播  
				sendBroadcast(mIntent);  
				QrActivity.this.finish();
			}
		});
	}

	@Override
	protected void onPause() {
		super.onPause();
		capture.stopCapture();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		capture.destroyCapture();
	}
	
	private class Listener implements CaptureListener{

		@Override
		public void handleDecode(Result obj, Bitmap barcode) {

//			inactivityTimer.onActivity();
//			playBeepSoundAndVibrate();
			String resultString = obj.getText();
			//FIXME
			if (resultString.equals("")) {
				Toast.makeText(QrActivity.this, "Scan failed!", Toast.LENGTH_SHORT).show();
			}else {
//				System.out.println("Result:"+resultString);
				/*Intent resultIntent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("result", resultString);
				resultIntent.putExtras(bundle);
				this.setResult(RESULT_OK, resultIntent);*/
				
				Intent mIntent = new Intent("OnSuccess");  
		        mIntent.putExtra("qrcode", resultString);  
		        //发送广播  
		        sendBroadcast(mIntent);  
			}
			QrActivity.this.finish();
			
		
		}
		
	}
	
	private class CallBack implements HandlerActivityCallBack{

		@Override
		public void activitySetResult(int resultCode, Intent data) {
			
		}

		@Override
		public void activityFinish() {
//			CaptureActivity.this.finish();
		}

		@Override
		public void activityStartActivity(Intent intent) {
			startActivity(intent);
		}
	}
	
}
